package com.hsj.common.system.controller;

import com.cjxch.supermybatis.base.bean.Pager;
import com.hsj.common.system.convert.SysUserConvert;
import com.hsj.common.system.entity.SysUserDo;
import com.hsj.common.system.service.ISysUserService;
import com.hsj.common.system.vo.request.ReqSysUserCreateVo;
import com.hsj.common.system.vo.request.ReqSysUserUpdateVo;
import com.hsj.common.system.vo.request.ReqSysUserPageQueryVo;
import com.hsj.common.system.vo.response.RespSysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * 用户 Controller
 * @Author: Czy
 * @Date: 2024-01-23 23:03:26
 * @Email: 314170122@qq.com
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController {

    private final ISysUserService sysUserService;
    private final SysUserConvert sysUserConvert;

    public SysUserController(ISysUserService sysUserService, SysUserConvert sysUserConvert) {
        this.sysUserService = sysUserService;
        this.sysUserConvert = sysUserConvert;
    }

    @PostMapping("createSysUser")
    @ApiOperation(value = "创建用户", notes = "创建用户", httpMethod = "POST")
    public Serializable createSysUser(@Valid @RequestBody ReqSysUserCreateVo vo){
        return sysUserService.insert(sysUserConvert.voToDo(vo));
    }

    @PostMapping("updateSysUser")
    @ApiOperation("修改用户")
    public Serializable updateSysUser(@Valid @RequestBody ReqSysUserUpdateVo vo){
        return sysUserService.update(sysUserConvert.voToDo(vo));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除用户")
    public Boolean delete(@PathVariable("id")String id){
        return sysUserService.delete(id) == 1L;
    }

    @PostMapping("/pageList")
    @ApiOperation("分页查询用户")
    public Pager<RespSysUserVo> pageList(@Valid @RequestBody ReqSysUserPageQueryVo vo){
        Pager<SysUserDo> pager = new Pager<>();
        pager.setPageNumber(vo.getPageNumber());
        pager.setPageSize(vo.getPageSize());
        pager = sysUserService.getPager(pager, vo.getSmCriteria());
        pager.setResult(sysUserConvert.doToVo(pager.getResult()));
        Pager<RespSysUserVo> retPager = new Pager<>();
        BeanUtils.copyProperties(pager, retPager);
        return retPager;
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取用户信息")
    public RespSysUserVo get(@PathVariable("id")String id) {
        return sysUserConvert.doToVo(sysUserService.get(id));
    }
}
